<?php
include_once "IArticleRepository.php";
include_once 'BaseSqlRepository.php';
include_once 'Article.php';
include_once 'Comment.php';
class SqlArticleRepository extends BaseSqlRepository implements IArticleRepository {
	private $url;
	private $port;
	private $username;
	private $password;
	public function getPreview($articleId) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "SELECT ArticleId, ATitle, LEFT(AContent, 200) as content  FROM ArticleT WHERE ArticleID =  ?" );
		if (! $stmt) {
			return NULL;
		}
		$stmt->bind_param ( "s", $articleID );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		
		$stmt->bind_result ( $articleId, $title, $content );
		
		$article = NULL;
		
		while ( $stmt->fetch () ) {
			
			$article = new Article ( NULL, $articleId, NULL );
			$article->setTitle ( $title );
			$article->setContent ( $content . "..." );
		}
		
		$stmt->close ();
		mysqli_close ( $mysqli );
		
		return $article;
	}
	public function getPreviews($from, $quantity) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "SELECT ArticleId, ATitle, LEFT(AContent, 200) as content FROM ArticleT LIMIT  ?,?" );
		if (! $stmt) {
			return NULL;
		}
		
		$fromRef = $from;
		$to = $fromRef + $quantity;
		
		$stmt->bind_param ( "ii", $fromRef, $to );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		
		$stmt->bind_result ( $articleId, $title, $content );
		
		$articles = array ();
		
		while ( $stmt->fetch () ) {
			
			$article = new Article ( NULL, $articleId, NULL );
			$article->setTitle ( $title );
			$article->setContent ( $content . "..." );
			$articles [] = $article;
		}
		
		$stmt->close ();
		mysqli_close ( $mysqli );
		
		return $articles;
	}
	public function getCount() {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		
		$query = "SELECT count(*) as count FROM ArticleT";
		
		$result = $mysqli->query ( $query );
		if (! $result) {
			mysqli_close ( $mysqli );
			return 0;
		}
		
		while ( $row = $result->fetch_assoc () ) {
			mysqli_close ( $mysqli );
			return $row ["count"];
		}
	}
	function __construct($databseUrl, $port, $username, $password) {
		$this->password = $password;
		$this->port = $port;
		$this->url = $databseUrl;
		$this->username = $username;
		$this->password = $password;
	}
	public function getArticle($articleID) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "SELECT ArticleId, ATitle, AContent, ACreator, ADate FROM ArticleT WHERE ArticleID =  ?" );
		if (! $stmt) {
			return NULL;
		}
		$stmt->bind_param ( "s", $articleID );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return NULL;
		}
		
		$stmt->bind_result ( $articleId, $title, $content, $userName, $publicationDate );
		
		$article = NULL;
		
		while ( $stmt->fetch () ) {
			
			$article = new Article ( $userName, $articleId, $publicationDate );
			$article->setTitle ( $title );
			$article->setContent ( $content );
			$article->addComments ( $this->getComments ( $articleId ) );
		}
		
		$stmt->close ();
		mysqli_close ( $mysqli );
		
		return $article;
	}
	public function addArticle(Article $article) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		
		$stmt = $mysqli->prepare ( "INSERT INTO ArticleT(ArticleID, AContent, ATitle, ACreator, ADate) VALUES (?,?,?,?,?)" );
		date_default_timezone_set ( "UTC" );
		$stmt->bind_param ( "sssss", $article->getArticleId (), $article->getContent (), $article->getTitle (), $article->getUserName (), date ( "Y-m-d H:i:s", $article->getPublicationDate () ) );
		
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
		public function editArticle(Article $article) {
			$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
			if ($mysqli->connect_errno) {
				return NULL;
			}
			$articleId = $article->getArticleId ();
			$title = $article->getTitle();
			$content = $article->getContent();
			echo $articleId;
			$stmt = $mysqli->prepare ( "UPDATE ArticleT SET AContent = ?, ATitle = ? WHERE ArticleId = ? "); //again, article ID is forced. Needs to be fixed...
			date_default_timezone_set ( "UTC" );
			
			$stmt->bind_param ( "sss", $content, $title, $articleId );
			if (! $stmt->execute ()) {
				mysqli_close ( $mysqli );
				return false;
			}
			$stmt->close ();
			mysqli_close ( $mysqli );
			
			return true;
	}
	private function getComments($articleId) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		
		$comments = array ();
		
		$stmtComment = $mysqli->prepare ( "SELECT CommentId, ArticleId, CCreator, CDate, CBody FROM CommentT WHERE ArticleId =  ?" );
		
		if (! $stmtComment) {
			return $comments;
		}
		$articlePass = $articleId;
		$stmtComment->bind_param ( "s", $articlePass );
		
		if (! $stmtComment->execute ()) {
			mysqli_close ( $mysqli );
			return $comments;
		}
		
		$stmtComment->bind_result ( $commentId, $articleId, $userName, $date, $content );
		
		while ( $stmtComment->fetch () ) {
			$comment = new Comment ( $content, $date, $userName, $articleId, $commentId );
			$comment->setId ( $commentId );
			$comments [] = $comment;
		}
		
		$stmtComment->close ();
		mysqli_close ( $mysqli );
		return $comments;
	}
	public function deleteArticle($articleId) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "DELETE FROM ArticleT WHERE ArticleID = ?" );
		if (! $stmt) {
			return false;
		}
		$stmt->bind_param ( "s", $articleId );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		
		$stmt = $mysqli->prepare ( "DELETE FROM CommentT WHERE ArticleID = ?" );
		if (! $stmt) {
			return false;
		}
		
		$stmt->bind_param ( "s", $articleId );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		
		mysqli_close ( $mysqli );
		return true;
	}
	public function addComment(Comment $comment) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		
		date_default_timezone_set ( "UTC" );
		
		$stmt = $mysqli->prepare ( "INSERT INTO CommentT( ArticleId, CCreator, CDate, CBody ) VALUES (?,?,?,?)" );
		$stmt->bind_param ( "ssss", $comment->getArticleId (), $comment->getUserName (), date ( "Y-m-d H:i:s", $comment->getPublicationDate () ), $comment->getContent () );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
	public function removeComment(Comment $comment) {
		$mysqli = new mysqli ( $this->url, $this->username, $this->password, Config::DATABASE_NAME );
		if ($mysqli->connect_errno) {
			return NULL;
		}
		$stmt = $mysqli->prepare ( "DELETE FROM CommentT WHERE CommentId = ?" );
		if (! $stmt) {
			return false;
		}
		$stmt->bind_param ( "i", $comment->getId () );
		if (! $stmt->execute ()) {
			mysqli_close ( $mysqli );
			return false;
		}
		$stmt->close ();
		mysqli_close ( $mysqli );
		return true;
	}
}

?>